/*****************************************************************************
 * gtk2.c : GTK+ plugin for vlc
 *****************************************************************************/

#include <stdlib.h>

#include <gtk/gtk.h>

#include <vlc/vlc.h>
#include <vlc_interface.h>
#include <vlc_playlist.h>

#include "intf.h"
#include "vlc_connect.h"
#include "gtk_extra/gtk_extra.h"

#include "../../../share/vlc32x32.xpm"

/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
static int  Open         ( vlc_object_t * );
static void Close        ( vlc_object_t * );
static int  OpenDialogs  ( vlc_object_t * );
static void Run          ( intf_thread_t * );
static void Init         ( intf_thread_t * );
static void ShowDialog   ( intf_thread_t *, int, int, intf_dialog_args_t * );

/*****************************************************************************
 * Local callbacks.
 *****************************************************************************/

static void window_destroy_cb  (GtkObject *object, gpointer user_data);

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/

vlc_module_begin();
    int i_score = getenv( "DISPLAY" ) == NULL ? 15 : 150;
    
    set_shortname( (char *)"Gtk2" );
    set_description( (char *)_("Gtk2 interface") );
    set_capability( "interface", i_score );
    set_category( CAT_INTERFACE );
    set_subcategory( SUBCAT_INTERFACE_MAIN );
    set_callbacks( Open, Close );
    set_program( "gvlc" );    
    
    add_submodule();
    set_description( _("Gtk+ dialogs provider") );
    set_capability( "dialogs provider", 50 );
    set_callbacks( OpenDialogs, Close );
    
vlc_module_end();

static int OpenDialogs( vlc_object_t *p_this )
{
    intf_thread_t *p_intf;
    int val;
    
    p_intf = (intf_thread_t *)p_this;
    val = Open( p_this );

    p_intf->pf_show_dialog = ShowDialog;

    return (val) ? val : VLC_SUCCESS;
}

static int Open( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;

    p_intf->pf_run = Run;

    p_intf->p_sys = (intf_sys_t *)malloc( sizeof(intf_sys_t) );
    if( !p_intf->p_sys )
    {
        msg_Err( p_intf, "Out of memory" );
        return VLC_ENOMEM;
    }
    memset( p_intf->p_sys, 0, sizeof(intf_sys_t) );

    p_intf->p_sys->p_playlist = pl_Yield( p_intf );

    p_intf->b_play = VLC_TRUE;

    return VLC_SUCCESS;
}

static void Close( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
    
    vlc_mutex_lock( &p_intf->object_lock );
    p_intf->b_dead = VLC_TRUE;
    vlc_mutex_unlock( &p_intf->object_lock );

    vlc_object_release( VLC_PLAYLIST );
    free( p_intf->p_sys );
}

/*****************************************************************************
 * Initialize the interface or the dialogs provider
 *****************************************************************************/

static void Run( intf_thread_t *p_intf )
{
    if( p_intf->pf_show_dialog )
    {
        if( vlc_thread_create( p_intf, "Gtk+ dialogs", Init, 0, VLC_TRUE ) )
            msg_Err( p_intf, "failed to create Gtk+ dialogs thread" );
    }
    else
    {
        Init( p_intf );
    }
}

static void Init( intf_thread_t *p_intf )
{
    GtkWidget *p_child_widget;
    GdkPixbuf *p_icon_pixbuf;

    gtk_set_locale();
    gtk_init_without_args();

    vlc_connect_init( p_intf );

    p_intf->p_sys->p_window = gtk_window_new( GTK_WINDOW_TOPLEVEL );

    p_icon_pixbuf = gdk_pixbuf_new_from_xpm_data( (const char **)vlc_xpm );

    gtk_window_set_title( GTK_WINDOW( p_intf->p_sys->p_window ), _( "VLC media player" ) );
    gtk_window_set_position( GTK_WINDOW ( p_intf->p_sys->p_window ), GTK_WIN_POS_CENTER );
    gtk_window_set_default_icon( p_icon_pixbuf );

    g_object_unref( p_icon_pixbuf );

    g_signal_connect( G_OBJECT( p_intf->p_sys->p_window ), "destroy", G_CALLBACK( window_destroy_cb ), p_intf );

    p_child_widget = create_main_window();
    gtk_container_add ( GTK_CONTAINER (p_intf->p_sys->p_window), p_child_widget );

    gtk_widget_show_all( p_intf->p_sys->p_window );

    if( p_intf->b_play != VLC_FALSE )
    {
        playlist_Control ( VLC_PLAYLIST, PLAYLIST_AUTOPLAY, VLC_FALSE );
    }
      
    gtk_main();
}

/*****************************************************************************
 * Callback to show a dialog
 *****************************************************************************/
static void ShowDialog( intf_thread_t *p_intf, int i_dialog_event, int i_arg,
                        intf_dialog_args_t *p_arg )
{

}

static void window_destroy_cb(GtkObject *object, gpointer user_data)
{
    gtk_main_quit();
  
    (void)user_data;
    (void)object;
}


